function [delay,time,filePath] = cal_latency(sat,gs,sampleTime,pl,outputFolder)
%cal_latency 绘制时延与时延变化率可视化
%   输入为卫星和地面站，输出为时延及其时间
    if nargin < 3
        pl = 1; % nargin是检查输入参数个数的函数，如果个数小于2，则b被设置为默认值1。
    end
   [delay,time] = latency(sat,gs);
    if pl==1
        fig=figure('Visible', 'off')
        plot(time,delay(1,:)*1000)                  % Plot in milliseconds
        xlim([time(1) time(end)])
        title("First Satellite's Latency vs. Time")
        xlabel("Simulation Time")
        ylabel("Latency (ms)")
        grid on
        % 创建输出目录（如果不存在）

    % 构造文件名并保存图像
    filename = fullfile(outputFolder, ...
        sprintf('latency_plot_%s_sat%d.png', gs.Name, sat.ID));
    saveas(fig, filename);
    close(fig);
    elseif pl==2
        fig=figure('Visible', 'off')
        subplot(2,1,1)
        plot(time,delay(1,:)*1000)                  % Plot in milliseconds
        xlim([time(1) time(end)])
        title("First Satellite's Latency vs. Time")
        xlabel("Simulation Time")
        ylabel("Latency (ms)")
        grid on
        subplot(2,1,2)
        latencyRate = diff(delay,1,2)/sampleTime;
        plot(time(1:end-1),latencyRate(1,:)*1e6)          % Plot in microseconds/second
        xlim([time(1) time(end-1)])
        title("First Satellite's Latency Rate vs. Time")
        xlabel("Simulation Time")
        ylabel("Latency Rate (\mus/s)")
        grid on
    filename = fullfile(outputFolder, ...
        sprintf('latency_plot_%s_sat%d.png', gs.Name, sat.ID));

    saveas(fig, filename);
    close(fig);
    filePath = filename;
    end

end